// $ANTLR 3.4 C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g 2013-02-27 21:42:49

  package practicaspl.doceuno;
    import java.util.Map;
    import java.util.HashMap;


import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;


@SuppressWarnings({"all", "warnings", "unchecked"})
public class Evaluador extends TreeParser {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "BLANCO", "CARACTER", "COLON", "COMA", "COMENTARIO", "C_CORCHETE", "C_PAR", "DIGITO", "DIV", "IDENT", "IGUAL", "MAS", "MAYOR", "MENOR", "MENOS", "MULT", "NUEVA_LINEA", "NUMERO", "O_CORCHETE", "O_PAR", "REAL", "SEMICOLON", "STRING", "TABULADOR", "CALL", "FUNC", "ID", "INSTRS", "LISTA_INST", "UNARIO", "ERROR"
    };

    public static final int EOF=-1;
    public static final int BLANCO=4;
    public static final int CARACTER=5;
    public static final int COLON=6;
    public static final int COMA=7;
    public static final int COMENTARIO=8;
    public static final int C_CORCHETE=9;
    public static final int C_PAR=10;
    public static final int DIGITO=11;
    public static final int DIV=12;
    public static final int IDENT=13;
    public static final int IGUAL=14;
    public static final int MAS=15;
    public static final int MAYOR=16;
    public static final int MENOR=17;
    public static final int MENOS=18;
    public static final int MULT=19;
    public static final int NUEVA_LINEA=20;
    public static final int NUMERO=21;
    public static final int O_CORCHETE=22;
    public static final int O_PAR=23;
    public static final int REAL=24;
    public static final int SEMICOLON=25;
    public static final int STRING=26;
    public static final int TABULADOR=27;
    public static final int CALL=28;
    public static final int FUNC=29;
    public static final int ID=30;
    public static final int INSTRS=31;
    public static final int LISTA_INST=32;
    public static final int UNARIO=33;
    public static final int ERROR=34;

    // delegates
    public TreeParser[] getDelegates() {
        return new TreeParser[] {};
    }

    // delegators


    public Evaluador(TreeNodeStream input) {
        this(input, new RecognizerSharedState());
    }
    public Evaluador(TreeNodeStream input, RecognizerSharedState state) {
        super(input, state);
    }

protected TreeAdaptor adaptor = new CommonTreeAdaptor();

public void setTreeAdaptor(TreeAdaptor adaptor) {
    this.adaptor = adaptor;
}
public TreeAdaptor getTreeAdaptor() {
    return adaptor;
}
    public String[] getTokenNames() { return Evaluador.tokenNames; }
    public String getGrammarFileName() { return "C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g"; }


      int historial = 0;
      boolean error = false;
      boolean errorunario=false;
      Map<String, ArrayList<Float>> historialID = new HashMap<String, ArrayList<Float>>();

      /** Points to functions tracked by tree builder. */
        private List<CommonTree> functionDefinitions;


        /** Remember local variables. Currently, this is only the function parameter.
         */
        private final Map<String, Float> localMemory = new HashMap<String, Float>();


        /** Remember global variables set by =. */
        private Map<String, Float> globalMemory = new HashMap<String, Float>();
        
        
        /** Set up an evaluator with a node stream; and a set of function definition ASTs. */
        public Evaluador(CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions) {
            this(nodes);
            this.functionDefinitions = functionDefinitions;
        }



      /** Set up a local evaluator for a nested function call. The evaluator gets the definition
         *  tree of the function; the set of all defined functions (to find locally called ones); a
         *  pointer to the global variable memory; and the value of the function parameter to be
         *  added to the local memory.
         */
        private Evaluador(CommonTree function,
                     List<CommonTree> functionDefinitions,
                     Map<String, Float> globalMemory,
                     Float paramValue) {
            // Expected tree for function: ^(FUNC IDENT ( INT | IDENT ) expr)
            this(new CommonTreeNodeStream(function.getChild(2)), functionDefinitions);
            this.globalMemory = globalMemory;
            localMemory.put(function.getChild(1).getText(), paramValue);
        }

        
         private CommonTree findFunction(String name, Float paramValue) {
            SEARCH:
            for (CommonTree f : functionDefinitions) {
                // Expected tree for f: ^(FUNC IDENT (IDENT | INT) expr)
                if (f.getChild(0).getText().equals(name)) {
                    // Check whether parameter matches
                    CommonTree formalPar = (CommonTree) f.getChild(1);
                    if (formalPar.getToken().getType() == NUMERO
                        && !new Float(formalPar.getToken().getText()).equals(paramValue)) {
                            // Constant in formalPar list does not match actual value -> no match.
                            continue SEARCH;
                    }
                    // Parameter (value for INT formal arg) as well as fct name agrees!
                    return f;
                }
            }
            return null;
        }
        

        /** Get value of name up call stack. */
        public float getValue(String name) {
          
            Float value = localMemory.get(name); 
                   
            if (value != null) {
                return value;
            }
            
            value = globalMemory.get(name);
            
            if (value != null) {
                return value;
            }
            
            // not found in local memory or global memory
            System.err.println("Variable " + name + " no definida.");
            return 0;
        }


    public static class instrucciones_return extends TreeRuleReturnScope {
        MiArbol tree;
        public Object getTree() { return tree; }
    };


    // $ANTLR start "instrucciones"
    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:106:1: instrucciones : ^( INSTRS ( tipoentrada )* ) ;
    public final Evaluador.instrucciones_return instrucciones() throws RecognitionException {
        Evaluador.instrucciones_return retval = new Evaluador.instrucciones_return();
        retval.start = input.LT(1);


        MiArbol root_0 = null;

        MiArbol _first_0 = null;
        MiArbol _last = null;

        MiArbol INSTRS1=null;
        Evaluador.tipoentrada_return tipoentrada2 =null;


        MiArbol INSTRS1_tree=null;

        try {
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:107:3: ( ^( INSTRS ( tipoentrada )* ) )
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:107:5: ^( INSTRS ( tipoentrada )* )
            {
            _last = (MiArbol)input.LT(1);
            {
            MiArbol _save_last_1 = _last;
            MiArbol _first_1 = null;
            _last = (MiArbol)input.LT(1);
            INSTRS1=(MiArbol)match(input,INSTRS,FOLLOW_INSTRS_in_instrucciones95); if (state.failed) return retval;


            if ( state.backtracking==0 )
            if ( _first_0==null ) _first_0 = INSTRS1;
            if ( input.LA(1)==Token.DOWN ) {
                match(input, Token.DOWN, null); if (state.failed) return retval;
                // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:107:14: ( tipoentrada )*
                loop1:
                do {
                    int alt1=2;
                    int LA1_0 = input.LA(1);

                    if ( ((LA1_0 >= DIV && LA1_0 <= MAS)||(LA1_0 >= MENOS && LA1_0 <= MULT)||LA1_0==NUMERO||LA1_0==REAL||(LA1_0 >= CALL && LA1_0 <= FUNC)||LA1_0==UNARIO) ) {
                        alt1=1;
                    }


                    switch (alt1) {
                	case 1 :
                	    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:107:14: tipoentrada
                	    {
                	    _last = (MiArbol)input.LT(1);
                	    pushFollow(FOLLOW_tipoentrada_in_instrucciones97);
                	    tipoentrada2=tipoentrada();

                	    state._fsp--;
                	    if (state.failed) return retval;
                	    if ( state.backtracking==0 ) 
                	     
                	    if ( _first_1==null ) _first_1 = tipoentrada2.tree;


                	    if ( state.backtracking==0 ) {
                	    retval.tree = (MiArbol)_first_0;
                	    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                	        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                	    }
                	    }
                	    break;

                	default :
                	    break loop1;
                    }
                } while (true);


                match(input, Token.UP, null); if (state.failed) return retval;
            }
            _last = _save_last_1;
            }


            if ( state.backtracking==0 ) {
            retval.tree = (MiArbol)_first_0;
            if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                retval.tree = (MiArbol)adaptor.getParent(retval.tree);
            }
            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return retval;
    }
    // $ANTLR end "instrucciones"


    public static class tipoentrada_return extends TreeRuleReturnScope {
        MiArbol tree;
        public Object getTree() { return tree; }
    };


    // $ANTLR start "tipoentrada"
    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:110:1: tipoentrada : (result= expr | asignacion | ^( FUNC ( . )+ ) );
    public final Evaluador.tipoentrada_return tipoentrada() throws RecognitionException {
        Evaluador.tipoentrada_return retval = new Evaluador.tipoentrada_return();
        retval.start = input.LT(1);


        MiArbol root_0 = null;

        MiArbol _first_0 = null;
        MiArbol _last = null;

        MiArbol FUNC4=null;
        MiArbol wildcard5=null;
        Evaluador.expr_return result =null;

        Evaluador.asignacion_return asignacion3 =null;


        MiArbol FUNC4_tree=null;
        MiArbol wildcard5_tree=null;

        try {
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:111:3: (result= expr | asignacion | ^( FUNC ( . )+ ) )
            int alt3=3;
            switch ( input.LA(1) ) {
            case DIV:
            case IDENT:
            case MAS:
            case MENOS:
            case MULT:
            case NUMERO:
            case REAL:
            case CALL:
            case UNARIO:
                {
                alt3=1;
                }
                break;
            case IGUAL:
                {
                alt3=2;
                }
                break;
            case FUNC:
                {
                alt3=3;
                }
                break;
            default:
                if (state.backtracking>0) {state.failed=true; return retval;}
                NoViableAltException nvae =
                    new NoViableAltException("", 3, 0, input);

                throw nvae;

            }

            switch (alt3) {
                case 1 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:111:5: result= expr
                    {
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_tipoentrada116);
                    result=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_0==null ) _first_0 = result.tree;


                    if ( state.backtracking==0 ) { System.out.println("Resultado Expresion: " + (result!=null?result.result:0.0f)); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 2 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:112:5: asignacion
                    {
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_asignacion_in_tipoentrada124);
                    asignacion3=asignacion();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_0==null ) _first_0 = asignacion3.tree;


                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 3 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:113:5: ^( FUNC ( . )+ )
                    {
                    _last = (MiArbol)input.LT(1);
                    {
                    MiArbol _save_last_1 = _last;
                    MiArbol _first_1 = null;
                    _last = (MiArbol)input.LT(1);
                    FUNC4=(MiArbol)match(input,FUNC,FOLLOW_FUNC_in_tipoentrada131); if (state.failed) return retval;


                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = FUNC4;
                    match(input, Token.DOWN, null); if (state.failed) return retval;
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:113:12: ( . )+
                    int cnt2=0;
                    loop2:
                    do {
                        int alt2=2;
                        int LA2_0 = input.LA(1);

                        if ( ((LA2_0 >= BLANCO && LA2_0 <= ERROR)) ) {
                            alt2=1;
                        }


                        switch (alt2) {
                    	case 1 :
                    	    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:113:12: .
                    	    {
                    	    _last = (MiArbol)input.LT(1);
                    	    wildcard5=(MiArbol)input.LT(1);

                    	    matchAny(input); if (state.failed) return retval;
                    	     
                    	    if ( state.backtracking==0 )
                    	    if ( _first_1==null ) _first_1 = wildcard5;


                    	    if ( state.backtracking==0 ) {
                    	    retval.tree = (MiArbol)_first_0;
                    	    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                    	        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    	    }
                    	    }
                    	    break;

                    	default :
                    	    if ( cnt2 >= 1 ) break loop2;
                    	    if (state.backtracking>0) {state.failed=true; return retval;}
                                EarlyExitException eee =
                                    new EarlyExitException(2, input);
                                throw eee;
                        }
                        cnt2++;
                    } while (true);


                    match(input, Token.UP, null); if (state.failed) return retval;
                    _last = _save_last_1;
                    }


                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return retval;
    }
    // $ANTLR end "tipoentrada"


    public static class asignacion_return extends TreeRuleReturnScope {
        MiArbol tree;
        public Object getTree() { return tree; }
    };


    // $ANTLR start "asignacion"
    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:116:1: asignacion : ^(oper= IGUAL id= IDENT exp= expr ) ;
    public final Evaluador.asignacion_return asignacion() throws RecognitionException {
        Evaluador.asignacion_return retval = new Evaluador.asignacion_return();
        retval.start = input.LT(1);


        MiArbol root_0 = null;

        MiArbol _first_0 = null;
        MiArbol _last = null;

        MiArbol oper=null;
        MiArbol id=null;
        Evaluador.expr_return exp =null;


        MiArbol oper_tree=null;
        MiArbol id_tree=null;

        try {
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:117:3: ( ^(oper= IGUAL id= IDENT exp= expr ) )
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:117:5: ^(oper= IGUAL id= IDENT exp= expr )
            {
            _last = (MiArbol)input.LT(1);
            {
            MiArbol _save_last_1 = _last;
            MiArbol _first_1 = null;
            _last = (MiArbol)input.LT(1);
            oper=(MiArbol)match(input,IGUAL,FOLLOW_IGUAL_in_asignacion151); if (state.failed) return retval;


            if ( state.backtracking==0 )
            if ( _first_0==null ) _first_0 = oper;
            match(input, Token.DOWN, null); if (state.failed) return retval;
            _last = (MiArbol)input.LT(1);
            id=(MiArbol)match(input,IDENT,FOLLOW_IDENT_in_asignacion155); if (state.failed) return retval;
             
            if ( state.backtracking==0 )
            if ( _first_1==null ) _first_1 = id;


            _last = (MiArbol)input.LT(1);
            pushFollow(FOLLOW_expr_in_asignacion159);
            exp=expr();

            state._fsp--;
            if (state.failed) return retval;
            if ( state.backtracking==0 ) 
             
            if ( _first_1==null ) _first_1 = exp.tree;


            match(input, Token.UP, null); if (state.failed) return retval;
            _last = _save_last_1;
            }


            if ( state.backtracking==0 ) { 
                  oper.setResult((exp!=null?exp.result:0.0f)); 
                System.out.println("Resultado Asignacion: " + id.getText() + " = " + (exp!=null?exp.result:0.0f));
                
                globalMemory.put(id.getText(), (exp!=null?exp.result:0.0f));

                ArrayList<Float> lista = new ArrayList<Float>();
                
                if (historialID.containsKey(id.getText())) {
                  lista = (ArrayList<Float>) historialID.get(id.getText()).clone(); 
                }
                
                lista.add((exp!=null?exp.result:0.0f));
                historialID.put(id.getText(),lista); 
              }

            if ( state.backtracking==0 ) {
            retval.tree = (MiArbol)_first_0;
            if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                retval.tree = (MiArbol)adaptor.getParent(retval.tree);
            }
            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return retval;
    }
    // $ANTLR end "asignacion"


    public static class parametro_return extends TreeRuleReturnScope {
        public float value = 0;
        MiArbol tree;
        public Object getTree() { return tree; }
    };


    // $ANTLR start "parametro"
    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:134:1: parametro returns [float value = 0] : (n= NUMERO |reg= REAL |id= IDENT -> {error}? ERROR -> IDENT );
    public final Evaluador.parametro_return parametro() throws RecognitionException {
        Evaluador.parametro_return retval = new Evaluador.parametro_return();
        retval.start = input.LT(1);


        MiArbol root_0 = null;

        MiArbol _first_0 = null;
        MiArbol _last = null;

        MiArbol n=null;
        MiArbol reg=null;
        MiArbol id=null;

        MiArbol n_tree=null;
        MiArbol reg_tree=null;
        MiArbol id_tree=null;
        RewriteRuleNodeStream stream_IDENT=new RewriteRuleNodeStream(adaptor,"token IDENT");

        try {
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:135:3: (n= NUMERO |reg= REAL |id= IDENT -> {error}? ERROR -> IDENT )
            int alt4=3;
            switch ( input.LA(1) ) {
            case NUMERO:
                {
                alt4=1;
                }
                break;
            case REAL:
                {
                alt4=2;
                }
                break;
            case IDENT:
                {
                alt4=3;
                }
                break;
            default:
                if (state.backtracking>0) {state.failed=true; return retval;}
                NoViableAltException nvae =
                    new NoViableAltException("", 4, 0, input);

                throw nvae;

            }

            switch (alt4) {
                case 1 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:135:5: n= NUMERO
                    {
                    _last = (MiArbol)input.LT(1);
                    n=(MiArbol)match(input,NUMERO,FOLLOW_NUMERO_in_parametro183); if (state.failed) return retval;
                     
                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = n;


                    if ( state.backtracking==0 ) { retval.value = Float.parseFloat(n.getText()); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 2 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:136:5: reg= REAL
                    {
                    _last = (MiArbol)input.LT(1);
                    reg=(MiArbol)match(input,REAL,FOLLOW_REAL_in_parametro193); if (state.failed) return retval;
                     
                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = reg;


                    if ( state.backtracking==0 ) { retval.value = Float.parseFloat(reg.getText()); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 3 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:137:5: id= IDENT
                    {
                    _last = (MiArbol)input.LT(1);
                    id=(MiArbol)match(input,IDENT,FOLLOW_IDENT_in_parametro203); if (state.failed) return retval; 
                    if ( state.backtracking==0 ) stream_IDENT.add(id);


                    if ( state.backtracking==0 ) { 
                        if (historial == 0) { 
                          retval.value = getValue(id.getText());
                        } else {
                          error = false;
                        
                          if (historialID.containsKey(id.getText())) {
                            ArrayList<Float> histID = new ArrayList<Float>();
                            histID = (ArrayList<Float>) historialID.get(id.getText()).clone();
                            
                            if (histID.size() - 1 < historial) {
                              error = true;
                            } else {
                              int pos = histID.size() - 1 - historial;
                            
                              Float valor = histID.get(pos);
                              retval.value = valor;
                            }
                          } else {
                            error = true;
                          }
                          
                          if (error) {    
                            errorunario = true;
                          
                            System.err.println("ERROR. No existe valor anterior para ese identificador");
                            retval.value = 0;
                          }
                        } 
                      }

                    // AST REWRITE
                    // elements: IDENT
                    // token labels: 
                    // rule labels: retval
                    // token list labels: 
                    // rule list labels: 
                    // wildcard labels: 
                    if ( state.backtracking==0 ) {

                    retval.tree = root_0;
                    RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null);

                    root_0 = (MiArbol)adaptor.nil();
                    // 167:3: -> {error}? ERROR
                    if (error) {
                        adaptor.addChild(root_0, 
                        (MiArbol)adaptor.create(ERROR, "ERROR")
                        );

                    }

                    else // 168:3: -> IDENT
                    {
                        adaptor.addChild(root_0, 
                        stream_IDENT.nextNode()
                        );

                    }


                    retval.tree = (MiArbol)adaptor.rulePostProcessing(root_0);
                    input.replaceChildren(adaptor.getParent(retval.start),
                                          adaptor.getChildIndex(retval.start),
                                          adaptor.getChildIndex(_last),
                                          retval.tree);
                    }

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return retval;
    }
    // $ANTLR end "parametro"


    public static class expr_return extends TreeRuleReturnScope {
        public float result = 0;
        MiArbol tree;
        public Object getTree() { return tree; }
    };


    // $ANTLR start "expr"
    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:171:1: expr returns [float result = 0] : ( parametro | call | ^(oper= MAS izq= expr der= expr ) | ^(oper= MENOS izq= expr der= expr ) | ^(oper= MULT izq= expr der= expr ) | ^(oper= DIV izq= expr der= expr ) | ^(oper= MENOS n= NUMERO ) | ^(oper= UNARIO e= expr ) );
    public final Evaluador.expr_return expr() throws RecognitionException {
        Evaluador.expr_return retval = new Evaluador.expr_return();
        retval.start = input.LT(1);


        MiArbol root_0 = null;

        MiArbol _first_0 = null;
        MiArbol _last = null;

        MiArbol oper=null;
        MiArbol n=null;
        Evaluador.expr_return izq =null;

        Evaluador.expr_return der =null;

        Evaluador.expr_return e =null;

        Evaluador.parametro_return parametro6 =null;

        Evaluador.call_return call7 =null;


        MiArbol oper_tree=null;
        MiArbol n_tree=null;

        try {
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:172:3: ( parametro | call | ^(oper= MAS izq= expr der= expr ) | ^(oper= MENOS izq= expr der= expr ) | ^(oper= MULT izq= expr der= expr ) | ^(oper= DIV izq= expr der= expr ) | ^(oper= MENOS n= NUMERO ) | ^(oper= UNARIO e= expr ) )
            int alt5=8;
            switch ( input.LA(1) ) {
            case IDENT:
            case NUMERO:
            case REAL:
                {
                alt5=1;
                }
                break;
            case CALL:
                {
                alt5=2;
                }
                break;
            case MAS:
                {
                alt5=3;
                }
                break;
            case MENOS:
                {
                int LA5_4 = input.LA(2);

                if ( (LA5_4==DOWN) ) {
                    int LA5_8 = input.LA(3);

                    if ( (LA5_8==NUMERO) ) {
                        int LA5_9 = input.LA(4);

                        if ( (LA5_9==UP) ) {
                            alt5=7;
                        }
                        else if ( ((LA5_9 >= DIV && LA5_9 <= IDENT)||LA5_9==MAS||(LA5_9 >= MENOS && LA5_9 <= MULT)||LA5_9==NUMERO||LA5_9==REAL||LA5_9==CALL||LA5_9==UNARIO) ) {
                            alt5=4;
                        }
                        else {
                            if (state.backtracking>0) {state.failed=true; return retval;}
                            NoViableAltException nvae =
                                new NoViableAltException("", 5, 9, input);

                            throw nvae;

                        }
                    }
                    else if ( ((LA5_8 >= DIV && LA5_8 <= IDENT)||LA5_8==MAS||(LA5_8 >= MENOS && LA5_8 <= MULT)||LA5_8==REAL||LA5_8==CALL||LA5_8==UNARIO) ) {
                        alt5=4;
                    }
                    else {
                        if (state.backtracking>0) {state.failed=true; return retval;}
                        NoViableAltException nvae =
                            new NoViableAltException("", 5, 8, input);

                        throw nvae;

                    }
                }
                else {
                    if (state.backtracking>0) {state.failed=true; return retval;}
                    NoViableAltException nvae =
                        new NoViableAltException("", 5, 4, input);

                    throw nvae;

                }
                }
                break;
            case MULT:
                {
                alt5=5;
                }
                break;
            case DIV:
                {
                alt5=6;
                }
                break;
            case UNARIO:
                {
                alt5=8;
                }
                break;
            default:
                if (state.backtracking>0) {state.failed=true; return retval;}
                NoViableAltException nvae =
                    new NoViableAltException("", 5, 0, input);

                throw nvae;

            }

            switch (alt5) {
                case 1 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:172:5: parametro
                    {
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_parametro_in_expr237);
                    parametro6=parametro();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_0==null ) _first_0 = parametro6.tree;


                    if ( state.backtracking==0 ) { retval.result = (parametro6!=null?parametro6.value:0.0f); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 2 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:173:5: call
                    {
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_call_in_expr245);
                    call7=call();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_0==null ) _first_0 = call7.tree;


                    if ( state.backtracking==0 ) { retval.result = (call7!=null?call7.value:0.0f); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 3 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:174:5: ^(oper= MAS izq= expr der= expr )
                    {
                    _last = (MiArbol)input.LT(1);
                    {
                    MiArbol _save_last_1 = _last;
                    MiArbol _first_1 = null;
                    _last = (MiArbol)input.LT(1);
                    oper=(MiArbol)match(input,MAS,FOLLOW_MAS_in_expr261); if (state.failed) return retval;


                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = oper;
                    match(input, Token.DOWN, null); if (state.failed) return retval;
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr269);
                    izq=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = izq.tree;


                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr273);
                    der=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = der.tree;


                    match(input, Token.UP, null); if (state.failed) return retval;
                    _last = _save_last_1;
                    }


                    if ( state.backtracking==0 ) { retval.result = (izq!=null?izq.result:0.0f) + (der!=null?der.result:0.0f); oper.setResult(retval.result); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 4 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:175:5: ^(oper= MENOS izq= expr der= expr )
                    {
                    _last = (MiArbol)input.LT(1);
                    {
                    MiArbol _save_last_1 = _last;
                    MiArbol _first_1 = null;
                    _last = (MiArbol)input.LT(1);
                    oper=(MiArbol)match(input,MENOS,FOLLOW_MENOS_in_expr285); if (state.failed) return retval;


                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = oper;
                    match(input, Token.DOWN, null); if (state.failed) return retval;
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr291);
                    izq=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = izq.tree;


                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr295);
                    der=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = der.tree;


                    match(input, Token.UP, null); if (state.failed) return retval;
                    _last = _save_last_1;
                    }


                    if ( state.backtracking==0 ) { retval.result = (izq!=null?izq.result:0.0f) - (der!=null?der.result:0.0f); oper.setResult(retval.result); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 5 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:176:5: ^(oper= MULT izq= expr der= expr )
                    {
                    _last = (MiArbol)input.LT(1);
                    {
                    MiArbol _save_last_1 = _last;
                    MiArbol _first_1 = null;
                    _last = (MiArbol)input.LT(1);
                    oper=(MiArbol)match(input,MULT,FOLLOW_MULT_in_expr307); if (state.failed) return retval;


                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = oper;
                    match(input, Token.DOWN, null); if (state.failed) return retval;
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr315);
                    izq=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = izq.tree;


                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr319);
                    der=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = der.tree;


                    match(input, Token.UP, null); if (state.failed) return retval;
                    _last = _save_last_1;
                    }


                    if ( state.backtracking==0 ) { retval.result = (izq!=null?izq.result:0.0f) * (der!=null?der.result:0.0f); oper.setResult(retval.result); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 6 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:177:5: ^(oper= DIV izq= expr der= expr )
                    {
                    _last = (MiArbol)input.LT(1);
                    {
                    MiArbol _save_last_1 = _last;
                    MiArbol _first_1 = null;
                    _last = (MiArbol)input.LT(1);
                    oper=(MiArbol)match(input,DIV,FOLLOW_DIV_in_expr331); if (state.failed) return retval;


                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = oper;
                    match(input, Token.DOWN, null); if (state.failed) return retval;
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr335);
                    izq=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = izq.tree;


                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr339);
                    der=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = der.tree;


                    match(input, Token.UP, null); if (state.failed) return retval;
                    _last = _save_last_1;
                    }


                    if ( state.backtracking==0 ) { retval.result = (izq!=null?izq.result:0.0f) / (der!=null?der.result:0.0f); oper.setResult(retval.result); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 7 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:178:5: ^(oper= MENOS n= NUMERO )
                    {
                    _last = (MiArbol)input.LT(1);
                    {
                    MiArbol _save_last_1 = _last;
                    MiArbol _first_1 = null;
                    _last = (MiArbol)input.LT(1);
                    oper=(MiArbol)match(input,MENOS,FOLLOW_MENOS_in_expr351); if (state.failed) return retval;


                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = oper;
                    match(input, Token.DOWN, null); if (state.failed) return retval;
                    _last = (MiArbol)input.LT(1);
                    n=(MiArbol)match(input,NUMERO,FOLLOW_NUMERO_in_expr355); if (state.failed) return retval;
                     
                    if ( state.backtracking==0 )
                    if ( _first_1==null ) _first_1 = n;


                    match(input, Token.UP, null); if (state.failed) return retval;
                    _last = _save_last_1;
                    }


                    if ( state.backtracking==0 ) { retval.result = -Float.parseFloat(n.getText()); oper.setResult(retval.result); }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;
                case 8 :
                    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:179:5: ^(oper= UNARIO e= expr )
                    {
                    _last = (MiArbol)input.LT(1);
                    {
                    MiArbol _save_last_1 = _last;
                    MiArbol _first_1 = null;
                    _last = (MiArbol)input.LT(1);
                    oper=(MiArbol)match(input,UNARIO,FOLLOW_UNARIO_in_expr378); if (state.failed) return retval;


                    if ( state.backtracking==0 )
                    if ( _first_0==null ) _first_0 = oper;
                    if ( state.backtracking==0 ) {if (historial == 0)
                              errorunario =false;
                            historial++; 
                          }

                    match(input, Token.DOWN, null); if (state.failed) return retval;
                    _last = (MiArbol)input.LT(1);
                    pushFollow(FOLLOW_expr_in_expr404);
                    e=expr();

                    state._fsp--;
                    if (state.failed) return retval;
                    if ( state.backtracking==0 ) 
                     
                    if ( _first_1==null ) _first_1 = e.tree;


                    match(input, Token.UP, null); if (state.failed) return retval;
                    _last = _save_last_1;
                    }


                    if ( state.backtracking==0 ) { 
                        if (error && historial>1) {
                          //System.out.println("ERROR en el operador UNARIO");
                          Tree defineNode = oper.getParent();
                          Tree stmtNode = defineNode.getParent();
                          stmtNode.deleteChild(defineNode.getChildIndex());
                        }
                        else {
                                
                          retval.result = (e!=null?e.result:0.0f);
                         } 
                         historial--;
                         
                         if (historial == 0)
                          errorunario = false;
                      }

                    if ( state.backtracking==0 ) {
                    retval.tree = (MiArbol)_first_0;
                    if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                        retval.tree = (MiArbol)adaptor.getParent(retval.tree);
                    }
                    }
                    break;

            }
        }
        catch (ArithmeticException ae) {

              
              
        }

        finally {
        	// do for sure before leaving
        }
        return retval;
    }
    // $ANTLR end "expr"


    public static class call_return extends TreeRuleReturnScope {
        public float value;
        MiArbol tree;
        public Object getTree() { return tree; }
    };


    // $ANTLR start "call"
    // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:206:1: call returns [float value] : ^(oper= CALL IDENT parametro ) ;
    public final Evaluador.call_return call() throws RecognitionException {
        Evaluador.call_return retval = new Evaluador.call_return();
        retval.start = input.LT(1);


        MiArbol root_0 = null;

        MiArbol _first_0 = null;
        MiArbol _last = null;

        MiArbol oper=null;
        MiArbol IDENT8=null;
        Evaluador.parametro_return parametro9 =null;


        MiArbol oper_tree=null;
        MiArbol IDENT8_tree=null;

        try {
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:207:5: ( ^(oper= CALL IDENT parametro ) )
            // C:\\Users\\Acer\\workspace\\doceuno\\src\\practicaspl\\doceuno\\Evaluador.g:207:7: ^(oper= CALL IDENT parametro )
            {
            _last = (MiArbol)input.LT(1);
            {
            MiArbol _save_last_1 = _last;
            MiArbol _first_1 = null;
            _last = (MiArbol)input.LT(1);
            oper=(MiArbol)match(input,CALL,FOLLOW_CALL_in_call439); if (state.failed) return retval;


            if ( state.backtracking==0 )
            if ( _first_0==null ) _first_0 = oper;
            match(input, Token.DOWN, null); if (state.failed) return retval;
            _last = (MiArbol)input.LT(1);
            IDENT8=(MiArbol)match(input,IDENT,FOLLOW_IDENT_in_call441); if (state.failed) return retval;
             
            if ( state.backtracking==0 )
            if ( _first_1==null ) _first_1 = IDENT8;


            _last = (MiArbol)input.LT(1);
            pushFollow(FOLLOW_parametro_in_call443);
            parametro9=parametro();

            state._fsp--;
            if (state.failed) return retval;
            if ( state.backtracking==0 ) 
             
            if ( _first_1==null ) _first_1 = parametro9.tree;


            match(input, Token.UP, null); if (state.failed) return retval;
            _last = _save_last_1;
            }


            if ( state.backtracking==0 ) { 
                  Float p = new Float((parametro9!=null?parametro9.value:0.0f));
                  
                CommonTree funcRoot = findFunction(IDENT8.getText(), p);
                
                if (funcRoot == null) {
                  System.err.println("No existe la llamada " + IDENT8.getText() + "(" + p + ")");
                } else {
                  Evaluador e = new Evaluador(funcRoot, functionDefinitions, globalMemory, p);
                  retval.value = e.expr().result;
                  oper.setResult(retval.value);
                }
              }

            if ( state.backtracking==0 ) {
            retval.tree = (MiArbol)_first_0;
            if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
                retval.tree = (MiArbol)adaptor.getParent(retval.tree);
            }
            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return retval;
    }
    // $ANTLR end "call"

    // Delegated rules


 

    public static final BitSet FOLLOW_INSTRS_in_instrucciones95 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_tipoentrada_in_instrucciones97 = new BitSet(new long[]{0x00000002312CF008L});
    public static final BitSet FOLLOW_expr_in_tipoentrada116 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_asignacion_in_tipoentrada124 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_FUNC_in_tipoentrada131 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_IGUAL_in_asignacion151 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_IDENT_in_asignacion155 = new BitSet(new long[]{0x00000002112CB000L});
    public static final BitSet FOLLOW_expr_in_asignacion159 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_NUMERO_in_parametro183 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_REAL_in_parametro193 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_IDENT_in_parametro203 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_parametro_in_expr237 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_call_in_expr245 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_MAS_in_expr261 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_expr_in_expr269 = new BitSet(new long[]{0x00000002112CB000L});
    public static final BitSet FOLLOW_expr_in_expr273 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_MENOS_in_expr285 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_expr_in_expr291 = new BitSet(new long[]{0x00000002112CB000L});
    public static final BitSet FOLLOW_expr_in_expr295 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_MULT_in_expr307 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_expr_in_expr315 = new BitSet(new long[]{0x00000002112CB000L});
    public static final BitSet FOLLOW_expr_in_expr319 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_DIV_in_expr331 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_expr_in_expr335 = new BitSet(new long[]{0x00000002112CB000L});
    public static final BitSet FOLLOW_expr_in_expr339 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_MENOS_in_expr351 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_NUMERO_in_expr355 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_UNARIO_in_expr378 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_expr_in_expr404 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_CALL_in_call439 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_IDENT_in_call441 = new BitSet(new long[]{0x0000000001202000L});
    public static final BitSet FOLLOW_parametro_in_call443 = new BitSet(new long[]{0x0000000000000008L});

}